package testjava;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class TestDb extends BaseTest {

	public static void main(String[] args) throws Exception {
		// testJdbc();
		testJpa();
	}

	static void testJdbc() throws ClassNotFoundException, SQLException {
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
				"CARETRACK_FULI", "CARETRACK_FULI");
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("select * from machines");
		while (rs.next()) {
			System.out.println(rs.getString(1));
		}
		rs.close();
		stmt.close();
		conn.close();
	}

	static void testJpa() throws ClassNotFoundException, SQLException {
		// EntityManagerFactory emf =
		// Persistence.createEntityManagerFactory("derby_embedded");
		// System.out.println(emf.getProperties());
		// EntityManager em = emf.createEntityManager();
		// Session session = em.unwrap(Session.class);
		// Connection conn = session.connection();
		// System.out.println(conn.getMetaData().getURL());
		// List list =
		// em.createNativeQuery("select st.* from stock_transaction st").getResultList();
		// System.out.println(list.size());
		// Object[] objArray = (Object[]) emf.createEntityManager()
		// .createNativeQuery("select * from stock where code='000960'").getSingleResult();
		// System.out.println(objArray);
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("oracle");
		System.out.println(emf.getProperties());
		EntityManager em = emf.createEntityManager();
		List<SupportType> list = em.createNamedQuery("findAllSupportTypes").getResultList();
		System.out.println(list);
	}

}
